use lib "../";
use BuildTool;
use Cwd;
use Data::Dumper;

use Test::More tests => 4;
use Test::Exception;
Test::More->builder->output ('Test_Report/Test_Cases/ts2_result.txt');
Test::More->builder->failure_output ('Test_Report/Test_Cases/ts2_errors.txt');

BEGIN {

 	use_ok(VCS);
 	use_ok(BuildTool);
 }
 
my $username; 
 
subtest 'Test Case 2.1' => sub {
	plan tests => 2;
####################################################################################
#	
#       Verifying first build detection
#
####################################################################################
	#Verifying First Build
	my $path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS2/TC2_1/";
	open(CMD,"whoami |");
	$username = <CMD>;
	chomp($username);
	#Checkout TC2_1 folder with no rel_h.txt - first build
	VCS->checkOutRelHis($path,$username);
	is(BuildTool->checkIsFirstBuild("TC2_1/rel_h.txt"),"true", "verifying that is the first build");
	system("rm -rf TC2_1");
	
	#Verifying Not First Build - path with the rel_h.txt file
	my $path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/branches/tags1/";
	#Checkout the tags folder with the existent release history file
	VCS->checkOutRelHis($path,$username);
	is(BuildTool->checkIsFirstBuild("tags1/release_history.txt"),"false", "verifying if it\'s NOT the first build");
	system("rm -rf tags1");

};

subtest 'Test Case 2.2' => sub {
	plan tests => 1;
####################################################################################
#	
# Verifying that release_history.txt was successfully created for the first build
#
####################################################################################
	#Initializing variables
	my $first_build = "true";
	my $path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/";
	#Checkout TC2_1 folder with no rel_h.txt - first build
	VCS->checkOutRelHis($path,$username);
	my $release_history_location = "tags/release_history.txt";
	Build->updateRelHist( $first_build,$release_history_location,"baseline","1.1");
	#Read the release_history.txt to check if the build tag is correct
	open(CMD,$release_history_location);
	my $base_number = <CMD>; 
	chomp($base_number);
	close CMD;
	is($base_number,"base1.1","Verifying build tag in release_history.txt");
	system("rm -rf tags");

};


